<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>17.3.BOM-定时器-setInterval()-停止定时器</title>
    <style>

    </style>
</head>

<body>
    <button class="begin">开启定时器</button>
    <button class="stop">停止定时器</button>
</body>

<script>
    window.addEventListener('load', function () {
        var begin = document.querySelector('.begin');
        var stop = document.querySelector('.stop');
        var timer = null; // 全局变量
        begin.addEventListener('click', function () {
            timer = setInterval(function () {
                console.log('你好');
            }, 1000)
        })
        stop.addEventListener('click', function () {
            clearInterval(timer);
        })
    })
</script>

</html>